{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 百度语音识别API调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import win32com.client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from aip import AipSpeech\n",
    " \n",
    "APP_ID = '21447324'\n",
    "API_KEY = 'bBg7bbWNDQVy9ggyjOvZfUdG'\n",
    "SECRET_KEY = 'wzALED5cIcguZf5RrVb9I3StM97arscz'\n",
    "client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)\n",
    "path = 'voices/question.wav'\n",
    " \n",
    "\n",
    "def listen():\n",
    "    # 读取录音文件\n",
    "    with open(path, 'rb') as fp:\n",
    "        voices = fp.read()\n",
    "    try:\n",
    "        result = client.asr(voices, 'wav', 16000, {'dev_pid': 1537, })\n",
    "        result_text = result[\"result\"][0]\n",
    "        print(\"you said: \" + result_text)\n",
    "        return result_text\n",
    "    except KeyError:\n",
    "        print(\"KeyError\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "you said: 我也不知道哦。\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'我也不知道哦。'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "listen()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 图灵机器人API调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "turing_api_key = \"db32944c370847ffa20dd1c8da0baced\"\n",
    "api_url = \"http://openapi.tuling123.com/openapi/api/v2\"  \n",
    "headers = {'Content-Type': 'application/json;charset=UTF-8'}\n",
    " \n",
    " \n",
    "# 图灵机器人回复\n",
    "def Turing(text_words=\"\"):\n",
    "    req = {\n",
    "        \"reqType\": 0,\n",
    "        \"perception\": {\n",
    "            \"inputText\": {\n",
    "                \"text\": text_words\n",
    "            },\n",
    " \n",
    "            \"selfInfo\": {\n",
    "                \"location\": {\n",
    "                    \"city\": \"北京\",\n",
    "                    \"province\": \"北京\",\n",
    "                    \"street\": \"车公庄\"\n",
    "                }\n",
    "            }\n",
    "        },\n",
    "        \"userInfo\": {\n",
    "            \"apiKey\": turing_api_key,\n",
    "            \"userId\": \"Nieson\"  \n",
    "        }\n",
    "    }\n",
    " \n",
    "    req[\"perception\"][\"inputText\"][\"text\"] = text_words\n",
    "    response = requests.request(\"post\", api_url, json=req, headers=headers)\n",
    "    response_dict = json.loads(response.text)\n",
    " \n",
    "    result = response_dict[\"results\"][0][\"values\"][\"text\"]\n",
    "    print(\"AI Robot said: \" + result)\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AI Robot said: 北京:周六 07月18日,雷阵雨转小雨 东风,最低气温22度，最高气温30度。\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'北京:周六 07月18日,雷阵雨转小雨 东风,最低气温22度，最高气温30度。'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Turing(text_words=\"今天天气如何？\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
